//package cn.xspace.wnx.service;
//
//import android.app.Service;
//import android.content.BroadcastReceiver;
//import android.content.Context;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.net.ConnectivityManager;
//import android.net.NetworkInfo;
//import android.os.Handler;
//import android.os.IBinder;
//import android.os.Message;
//import android.widget.Toast;
//
//import com.xspace.android.xafcommon.util.AppUtils;
//import com.xspace.android.xafcommon.util.Constants;
//import com.xspace.android.xafcommon.util.LogUtil;
//import cn.xspace.library.util.MyInfoUtil;
//import com.xspace.android.xafcommon.util.PreferencesUtils;
//
//import org.jivesoftware.smack.XMPPConnection;
//import org.jivesoftware.smack.XMPPException;
//import org.jivesoftware.smack.packet.Presence;
//
//import cn.xspace.wnx.R;
//import com.xspace.android.xafcommon.appliction.BaseAppliction;
//import cn.xspace.wnx.util.ThreadPoolManager;
//import cn.xspace.wnx.xmpp.XmppConnectListener;
//import cn.xspace.wnx.xmpp.XmppConnectionManager;
//
///**
// * 重连接服务.
// *
// * @author
// */
//public class ReConnectService extends Service {
//
//    private static final String TAG = "ReConnectService";
//
//    private Context mContext;
//    private ConnectivityManager connectivityManager;
//    private NetworkInfo info;
//    private final static int MSG_CONNENT = 0;
//    private final static int MSG_RECONNECT = 1;
//    private int curTime = 0;
//    public static int MAX_REONNNECT_TIMES = 10;
//
//    private boolean isConnection = false;
//
//    @Override
//    public void onCreate() {
//        mContext = this;
//        IntentFilter mFilter = new IntentFilter();
//        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
//        registerReceiver(reConnectionBroadcastReceiver, mFilter);
//        super.onCreate();
//    }
//
//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        return super.onStartCommand(intent, flags, startId);
//    }
//
//    @Override
//    public IBinder onBind(Intent intent) {
//        return null;
//    }
//
//    @Override
//    public void onDestroy() {
//        unregisterReceiver(reConnectionBroadcastReceiver);
//        super.onDestroy();
//    }
//
//    BroadcastReceiver reConnectionBroadcastReceiver = new BroadcastReceiver() {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            String action = intent.getAction();
//            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
//                mHandler.obtainMessage(MSG_RECONNECT).sendToTarget();
//            }
//        }
//    };
//
//
//    class ConnectionRunnable extends Thread {
//
//        public XMPPConnection connection;
//
//        public ConnectionRunnable(XMPPConnection cn) {
//            this.connection = cn;
//        }
//
//        @Override
//        public void run() {
//            isConnection = true;
//            reConnect(connection);
//        }
//
//        /**
//         * 递归重连，直连上为止.
//         *
//         * @author shimiso
//         * @update 2012-7-10 下午2:12:25
//         */
//        public void reConnect(XMPPConnection connection) {
//            try {
//                connection.addConnectionListener(new XmppConnectListener(mContext));
//                connection.connect();
//                if (connection.isConnected()) {
//                    String userid = MyInfoUtil.getMyUserid(mContext);
//                    String pasword = AppUtils.MD5((String) PreferencesUtils.getPfValue(BaseAppliction.mInstance, Constants.ACCOUNT_PASSWORD_KEY,
//                            String.class.getName()), false);
//                    LogUtil.d(userid);
//                    LogUtil.d(pasword);
//                    if (!connection.isAuthenticated()) {
//                        connection.login(userid, pasword, XmppConnectionManager.ACCOUNT_RESOURCE_KEY); // 登录
//                    }
//                    Presence presence = new Presence(Presence.Type.available);
//                    connection.sendPacket(presence);
//                    mHandler.obtainMessage(MSG_CONNENT).sendToTarget();
//                }
//            } catch (XMPPException e) {
//                e.printStackTrace();
//                LogUtil.e(TAG,"wubaojie>>>" + "reConnect: " + "XMPP连接失败!");
//                if (curTime < MAX_REONNNECT_TIMES) {
//                    curTime++;
//                    reConnect(connection);
//                } else {
//                    //重新连接10后不再重新连接,等待下次网络状态变化
//                    isConnection = false;
//                    curTime = 0;
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//
//    }
//
//    private Handler mHandler = new Handler(new Handler.Callback() {
//
//        @Override
//        public boolean handleMessage(Message msg) {
//            switch (msg.what) {
//                case MSG_CONNENT:
//                    //连接成功
//                    curTime = 0;
//                    LogUtil.e(TAG, "wubaojie>>>" + "handleMessage: MSG_CONNENT : " + "连接成功");
//                    Toast.makeText(mContext, R.string.user_on_line, Toast.LENGTH_LONG).show();
//                    break;
//                case MSG_RECONNECT:
//                    LogUtil.e(TAG, "wubaojie>>>" + "handleMessage: MSG_RECONNECT :" + "网络状态已经改变");
//                    connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
//                    XMPPConnection connection = XmppConnectionManager.getInstance().getConnection();
//                    info = connectivityManager.getActiveNetworkInfo();
//                    if (info != null && info.isAvailable()) {
//                        if (!connection.isConnected()) {
//                            LogUtil.e(TAG, "wubaojie>>>" + "handleMessage: MSG_RECONNECT :" + "用户已离线");
//                            Toast.makeText(mContext, R.string.user_off_line, Toast.LENGTH_LONG).show();
//                            if (!isConnection) {
//                                ConnectionRunnable connRunnable = new ConnectionRunnable(connection);
//                                ThreadPoolManager.getInstance().addExecuteTask(connRunnable);
//                            }
//                        } else {
//                            LogUtil.e(TAG, "wubaojie>>>" + "handleMessage: MSG_RECONNECT :" + "用户已上线");
//                            Toast.makeText(mContext, R.string.user_on_line, Toast.LENGTH_LONG).show();
//                        }
//                    } else {
//                        LogUtil.e(TAG, "wubaojie>>>" + "handleMessage: MSG_RECONNECT :" + "网络断开,用户已离线!");
//                        Toast.makeText(mContext, R.string.network_off_user_off_line, Toast.LENGTH_LONG).show();
//                    }
//                    break;
//            }
//            return false;
//        }
//    });
//
//    private void sendInentAndPre(boolean isSuccess) {
//        Intent intent = new Intent();
//        // SharedPreferences preference =
//        // getSharedPreferences(Constant.LOGIN_SET,
//        // 0);
//        // // 保存在线连接信息
////		 preference.edit().putBoolean(Constant.IS_ONLINE, isSuccess).commit();
////        intent.setAction(Constant.ACTION_RECONNECT_STATE);
//        // intent.putExtra(Constant.RECONNECT_STATE, isSuccess);
////        sendBroadcast(intent);
//    }
//
//}
